package com.axend.lib_base.view.dialog

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.app.Dialog
import android.content.Context
import android.view.View
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import com.axend.lib_base.R
import com.axend.lib_base.databinding.BaseDialogCustomAlarmBinding
import com.axend.lib_base.ext.dp2px
import com.axend.lib_base.ext.windowWidth
import com.axend.lib_base.utils.AudioPlayer


/**
 * @description自定义报警弹窗
 * @author admins
 * @time 2024/7/2 15:51
*/
class CustomAlarmDialog(context: Context): Dialog(context, R.style.Common_CustomAlarmDialog) {

    private var dialogBinding: BaseDialogCustomAlarmBinding = DataBindingUtil.inflate(
        layoutInflater,
        R.layout.base_dialog_custom_alarm,
        null,
        false
    )
    private val blinkAnimationMask by lazy {
        createBlinkAnimation(dialogBinding.mhvMask)
    }

    private val blinkAnimationImage by lazy {
        createBlinkAnimation(dialogBinding.ivAlarm)
    }

    private var animatorSet: AnimatorSet? = null

    private var alarmType:String=""

    companion object {
        @Volatile
        private var instance: CustomAlarmDialog? = null

        fun getInstance(context: Context): CustomAlarmDialog {
            return instance ?: synchronized(this) {
                instance ?: createInstance(context).also { instance = it }
            }
        }

        private fun createInstance(context: Context): CustomAlarmDialog {
            return CustomAlarmDialog(context).apply {
                // 初始化Dialog的配置，比如设置视图、按钮监听器等
                setCancelable(true)
                // 这里可以根据需要设置对话框的内容，例如使用自定义布局
            }
        }
    }


    init {
        setContentView(dialogBinding.root)
        setCanceledOnTouchOutside(false)
        val window = window
        window?.apply {
            val layoutParams = window.attributes
            layoutParams.dimAmount = 0.5f // 设置透明度
            window.attributes = layoutParams
            clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
        }
        //设置提示框位置
        val lp = window!!.attributes
        //lp.gravity = Gravity.TOP | Gravity.LEFT;
        //lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
        //lp.x = DisplayUtils.dip2px(53);
        //lp.y = DisplayUtils.dip2px(186);
        lp.width = context.windowWidth()
        window.attributes = lp

        dialogBinding.ivClose.setOnClickListener {
            dismiss()
            AudioPlayer.getInstance(context).stop()
        }

        dialogBinding.btOk.setOnClickListener {
            dismiss()
            AudioPlayer.getInstance(context).stop()
        }

    }

    /**
     * 显示弹窗
     */
    fun show(tips: String,alarmType:String="") :Dialog{
        dialogBinding.tvAlarm.text = tips
        animatorSet= AnimatorSet().apply {
            playTogether(blinkAnimationMask, blinkAnimationImage)
            start()
        }
        AudioPlayer.getInstance(context).play(R.raw.alarm)
        this.alarmType=alarmType
        this.show()
        return this@CustomAlarmDialog
    }


    private fun createBlinkAnimation(T : View): ObjectAnimator {
        return ObjectAnimator.ofFloat(
            T,
            View.ALPHA,
            0f, 1f
        ).apply {
            duration = 500
            repeatCount = ValueAnimator.INFINITE
            repeatMode = ValueAnimator.REVERSE
            addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    super.onAnimationEnd(animation)
                }

            })
        }
    }



    override fun dismiss() {
        if (this.isShowing)AudioPlayer.getInstance(context).stop()
        super.dismiss()
        if (animatorSet?.isRunning == true) {
            animatorSet?.cancel()
        }
    }


    fun getAlarmType():String{
        return alarmType
    }
}